package com.leyou.item.web;

import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.CategoryService;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("spec")
public class SpecController {


    private SpecGroupService groupService;
    private SpecParamService paramService;


    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;

    }
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(
            @RequestParam("id") Long id){
        return ResponseEntity.ok(
                SpecGroupDTO.convertEntityList(
                        groupService.query().eq("category_id", id).list()
                )
        );
    }

    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ){
        return ResponseEntity.ok(paramService.queryParams(categoryId, groupId, searching));
    }

    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupAndDetailByCategories(
            @RequestParam("id") Long id){
        List<SpecGroupDTO> specGroupDTOS = groupService.queryGroupAndDetailByCategories(id);
        return ResponseEntity.ok(specGroupDTOS);
    }
}